package com.yunfeisoft.controller.business;

import com.alibaba.fastjson.JSON;
import com.applet.base.BaseController;
import com.yunfeisoft.business.model.SmsHomeAdvertise;
import com.yunfeisoft.business.service.inter.SmsHomeAdvertiseService;
import com.applet.utils.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: SmsHomeAdvertiseController
 * Description: 首页轮播广告表Controller
 * Author: Jackie liu
 * Date: 2020-06-02
 */
@Controller
public class SmsHomeAdvertiseController extends BaseController {

    @Autowired
    private SmsHomeAdvertiseService smsHomeAdvertiseService;

    /**
     * 添加首页轮播广告表
     *
     * @param advertiseStr
     * @param attachmentId
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/smsHomeAdvertise/save", method = RequestMethod.POST)
    @ResponseBody
    public Response save(String advertiseStr, String attachmentId,  HttpServletRequest request, HttpServletResponse response) {
        SmsHomeAdvertise advertise = JSON.parseObject(advertiseStr, SmsHomeAdvertise.class);
        if (ObjectUtils.isEmpty(advertise)) {
            return ResponseUtils.success("参数错误");
        }
        if (StringUtils.isBlank(advertise.getName())) {
            return ResponseUtils.success("广告名称为空");
        }
        if (ObjectUtils.isEmpty(advertise.getStartDate())) {
            return ResponseUtils.success("开始时间为空");
        }
        if (ObjectUtils.isEmpty(advertise.getEndDate())) {
            return ResponseUtils.success("结束时间为空");
        }
        if (ObjectUtils.isEmpty(advertise.getStatus())) {
            return ResponseUtils.success("广告状态为空");
        }
        if (ObjectUtils.isEmpty(advertise.getType())) {
            return ResponseUtils.success("广告类型为空");
        }
        if (StringUtils.isBlank(attachmentId)) {
            return ResponseUtils.success("图片有误");
        }
        if (StringUtils.isBlank(advertise.getUrl())) {
            return ResponseUtils.success("广告链接地址为空");
        }
        if (ObjectUtils.isEmpty(advertise.getSort())) {
            return ResponseUtils.success("排序为空");
        }
        smsHomeAdvertiseService.saveAdvertise(advertise, attachmentId);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 修改首页轮播广告表
     *
     * @param advertiseStr
     * @param attachmentId
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/smsHomeAdvertise/modify", method = RequestMethod.POST)
    @ResponseBody
    public Response modify(String advertiseStr, String attachmentId, HttpServletRequest request, HttpServletResponse response) {
        SmsHomeAdvertise advertise = JSON.parseObject(advertiseStr, SmsHomeAdvertise.class);
        if (ObjectUtils.isEmpty(advertise)) {
            return ResponseUtils.success("参数错误");
        }
        if (StringUtils.isBlank(advertise.getId())) {
            return ResponseUtils.success("id不能为空");
        }
        if (StringUtils.isBlank(advertise.getName())) {
            return ResponseUtils.success("广告名称为空");
        }
        if (ObjectUtils.isEmpty(advertise.getStartDate())) {
            return ResponseUtils.success("开始时间为空");
        }
        if (ObjectUtils.isEmpty(advertise.getEndDate())) {
            return ResponseUtils.success("结束时间为空");
        }
        if (ObjectUtils.isEmpty(advertise.getStatus())) {
            return ResponseUtils.success("广告状态为空");
        }
        if (ObjectUtils.isEmpty(advertise.getType())) {
            return ResponseUtils.success("广告类型为空");
        }
        if (StringUtils.isBlank(attachmentId)) {
            return ResponseUtils.success("图片有误");
        }
        if (StringUtils.isBlank(advertise.getUrl())) {
            return ResponseUtils.success("广告链接地址为空");
        }
        if (ObjectUtils.isEmpty(advertise.getSort())) {
            return ResponseUtils.success("排序为空");
        }
        smsHomeAdvertiseService.modifyAdvertise(advertise, attachmentId);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 查询首页轮播广告表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/smsHomeAdvertise/query", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response query(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        SmsHomeAdvertise record = smsHomeAdvertiseService.load(id);
        return ResponseUtils.success(record);
    }

    /**
     * 分页查询首页轮播广告表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/smsHomeAdvertise/list", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response list(HttpServletRequest request, HttpServletResponse response) {
        String name = ServletRequestUtils.getStringParameter(request, "name", null);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("name", name);
        initParams(params, request);
        Page<SmsHomeAdvertise> page = smsHomeAdvertiseService.queryPage(params);
        return ResponseUtils.success(page);
    }

    /**
     * 批量删除首页轮播广告表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/smsHomeAdvertise/delete", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response delete(HttpServletRequest request, HttpServletResponse response) {
        String ids = ServletRequestUtils.getStringParameter(request, "ids", null);
        if (StringUtils.isBlank(ids)) {
            return ResponseUtils.warn("参数错误");
        }
        smsHomeAdvertiseService.remove(ids.split(","));
        return ResponseUtils.success("删除成功");
    }
}
